/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {Thrift} from '@ohos/thrift'
import CalculatorClient from '../../common/calculator/Calculator';
import {WorkBool, WorkBinary, WorkString, Operation
} from '../../common/calculator/tutorial_types';

export default {
    data: {
        stringAppend: '',
        boolReverse: '',
        binaryOperation: ''
    },
    onInit() {
    },
    onButtonClick() {
        var transport = new Thrift.Transport("http://192.168.0.109:9090/thrift/service/tutorial/");
        var protocol = new Thrift.Protocol(transport);
        var client = new CalculatorClient(protocol);

        var that = this;

        var workString = new WorkString()
        workString.value1 = 'Thrift';
        workString.value2 = '_OHOS';
        workString.op = Operation.APPEND;
        client.performAppendString(1, workString, function (result) {
            if (result) {
                that.stringAppend = result;
            }
        });

        var workBool = new WorkBool()
        workBool.value = false;
        workBool.op = Operation.REVERSE_BOOLEAN;
        client.performReverseBoolean(1, workBool, function (result) {
            if (result) {
                that.boolReverse = result;
            }
        });

        var workBinary = new WorkBinary();
        workBinary.value = 'aGk='; // encode base 64 value
        workBinary.op = Operation.BINARY_SIZE;
        client.performByteArraySize(1, workBinary, function (result) {
            if (result) {
                that.binaryOperation = result;
            }
        });
    }
}
